<?php


namespace api\portal\controller;


use Cassandra\Date;
use cmf\controller\RestBaseController;
use think\Db;
use think\Exception;

class OrderController extends RestBaseController
{
    public function index()
    {
        header('Access-Control-Allow-Origin: *');
        try {
            $requst = $this->request;
            $list_rows = $requst->param("list_rows");
            $name = $this->getChineseGetValue($_GET["name"]);
            $phone = $this->getChineseGetValue($_GET["phone"]);
            $userId = $requst->param("userId");
            $token = $requst->param("token");
            $this->LogInOrNot($userId, $token);
            $data = Db::name("orders")->alias("o")
                ->field("o.id,o.order_sn,o.customer_name,o.customer_phone,o.tomb_num,o.tomb_address,FROM_UNIXTIME(o.end_time) end_time,o.order_status,p.dtype pstatus,p.status pstatus2, or.status orstatus,p.id pid")//,p.status pstatus,or.status orstatus,p.id pid
                ->join("order or", "o.id = or.orders_id", "left")
                ->join("pay_deposit p", "or.order_id = p.order_id", "left")
//            ->where("o.retentiondeadline", "<", time())
                ->where("o.edit_user", $userId)
                ->where("o.order_status", "<>", "2")
//            ->where("p.status","in",['1','2'])
                ->where("o.customer_name", 'like', '%' . $name . '%')
                ->where("o.customer_phone", 'like', '%' . $phone . '%')
                ->order("o.add_time","desc")
                ->paginate($list_rows, false, ['query' => request()->param()])->toArray();
//        $sql = Db::name("orders")->getLastSql();
//        echo $sql;
//        $this->success("ss",$sql);

            if (!empty($data)) {
//            print_r($data['data']);die;
                for ($i = 0; $i < count($data['data']); $i++) {
//                echo count($data['data']);
//                    if ($data['data'][$i]["order_status"] == '1') {
//                        if ($data['data'][$i]["pstatus"] == '1') {
//                            $data['data'][$i]["order_status"] = "交定金";
//                        } else if ($data['data'][$i]["pstatus"] == 0 || $data['data'][$i]["pid"] == null) {
//                            if ($data['data'][$i]["end_time"] > time()) {
//                                $data['data'][$i]["order_status"] = "保留";
//                            } else {
//                                unset($data['data'][$i]);
//                                $data['data'] = array_values($data['data']);
//                            }
//                        } else {
//                            $data['data'][$i]["order_status"] = "已认购";
//                        }
//                    } elseif ($data['data'][$i]["order_status"] == '3') {
//                        if ($data['data'][$i]["orstatus"] == '3') {
//                            $data['data'][$i]["order_status"] = "退款";
//                        } elseif ($data['data'][$i]["orstatus"] == '4') {
//                            $data['data'][$i]["order_status"] = "退墓";
//                        } else {
//                            $data['data'][$i]["order_status"] = "已完款";
//                        }
//                    } else {
//                        unset($data['data'][$i]);
//                        $data['data'] = array_values($data['data']);
//                    }
                    if ($data['data'][$i]["order_status"] == "1") {
                        if ($data['data'][$i]['pstatus'] == 1 && $data['data'][$i]['pstatus2'] ==1) {
                            $data['data'][$i]["order_status"] = "定金";
                        } else if ($data['data'][$i]['pstatus'] == 2) {
                            $data['data'][$i]["order_status"] = "退定";
                        } else {
                            $data['data'][$i]["order_status"] = "已认购";
                        }
                    }elseif ($data['data'][$i]["order_status"] == "3"){
                        $data['data'][$i]["order_status"] = "已完款";
                    }


                }
                $response = ['list' => $data];
            } else {
                $response = $data;
            }
            $this->success("请求成功！", $response);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }

    }

}